function [varargout]=Equ_Portfolio(PP,SS,MODEL,JacobianFlag,Dev_Input,Dev_Output,TimePrintFlag)

%% Preliminaries
VAR         =   MODEL.VAR;
EQ          =   MODEL.EquBlock.Portfolio;

if nargin<=3
    JacobianFlag    =   0;
end
if nargin<=4
    Dev_Input       =   zeros(EQ.Dim.In,1);
    Dev_Output      =   zeros(EQ.Dim.Out,1);
elseif ( isempty(Dev_Input) || isempty(Dev_Output) )
    Dev_Input       =   zeros(EQ.Dim.In,1);
    Dev_Output      =   zeros(EQ.Dim.Out,1);
end
if nargin<=6
    TimePrintFlag   =   0;
end
if TimePrintFlag
    TimeStart       =   tic;
end

%% Evaluation
%--------------------------------------------------------------------------
% Pre-Transformation: Input Deviations to Input Levels
%--------------------------------------------------------------------------

for ii=1:EQ.Num.In
    TempVar     =   EQ.Var.In{ii};
    TempDev     =   Dev_Input(EQ.LocIdx.In.(TempVar));
    TempLevel   =   VAR.(TempVar).Dev2Level(TempDev);
    eval(['Dev_',TempVar,'=TempDev;'])
    eval([TempVar,'=TempLevel;'])
end

%--------------------------------------------------------------------------
% Evaluation
%--------------------------------------------------------------------------
[Bp_ext,KAPPA,q_dom, q_ext] ...
                =   PortfolioAdjCost('r_dom',ir_dom,'r_ext',ir_ext,...
                                     'ExtBond_Target',PP.Fin_ExtBond,...
                                     'AdjCost',PP.Fin_AdjCost).OptChoice;
PortfolioMat    =   [Bp_ext;KAPPA;q_dom; q_ext];
PortfolioInfo   =   PortfolioMat(:);
Lag_PortfolioInfop= PortfolioInfo;

%--------------------------------------------------------------------------
% Post-Transformation: Input Levels to Input Deviations
%--------------------------------------------------------------------------
Dev_Left    =   zeros(EQ.Dim.Out,1);
for ii=1:EQ.Num.Out
    TempVar     =   EQ.Var.Out{ii};
    eval(['TempLevel=',TempVar,';']);
    TempDev     =   VAR.(TempVar).Level2Dev(TempLevel);
    Dev_Left(EQ.LocIdx.Out.(TempVar)) ...
                =   TempDev;
end
%% Return Objectives

switch JacobianFlag
    case 0
        varargout{1}    =   -Dev_Output+Dev_Left;
    case 1
        TempFun         =   @(Dev_Input)Equ_Portfolio(PP,SS,MODEL,0,Dev_Input,Dev_Output);
        Jac             =   struct();
        Jac.Input       =   parfdjac(TempFun,Dev_Input);
        Jac.Output      =   -speye(EQ.Dim.Out);
        
        for ii=1:EQ.Num.In
            TempVar         =   EQ.Var.In{ii};
            Jac.(TempVar)   =   Jac.Input(:,EQ.LocIdx.In.(TempVar));
        end
        for ii=1:EQ.Num.Out
            TempVar         =   EQ.Var.Out{ii};
            Jac.(TempVar)   =   Jac.Output(:,EQ.LocIdx.Out.(TempVar));
        end
        varargout{1}    =   Jac;
end
if TimePrintFlag
    TimeElapsed     =   toc(TimeStart);
    fprintf(['Jacobian of Portfolio Block is done: ',...
             'Elapse Time=',num2str(TimeElapsed,'%.2f'),'s','\n']);
end

